package com.threeti.mecool.common.resource.core;

import java.io.IOException;

public class ContentException extends IOException {
	
	private static final long serialVersionUID = 1L;
	
	private int statusCode;
	private String responsePhrase;
	private String url;

	public ContentException(Exception ex) {
		this.initCause(ex);
	}

	public ContentException(String msg, String url) {
		this(msg, url, -1, null, null);
	}
	public ContentException(String msg, String url, Exception initCause) {
		this(msg, url, -1, null, initCause);
	}

	public ContentException(String msg, String url, int statusCode, String responsePhrase) {
		this(msg, url, statusCode, responsePhrase, null);
	}
	public ContentException(String url, int statusCode, String responsePhrase) {
		this("The server has returned an HTTP error", url, statusCode, responsePhrase, null);
	}

	/**
	 * @param msg			Custom description of failure
	 * @param statusCode	 Error code returned by server
	 * @param responsePhrase Response phrase following the error code
	 */
	public ContentException(String msg, String url, int statusCode, String responsePhrase, Exception initCause) {
		super(msg + responsePhrase != null ? ", response: " + responsePhrase : "" + ", statusCode: " + statusCode);
		this.url = url;
		this.statusCode = statusCode;
		this.responsePhrase = responsePhrase;
		if (initCause != null)
			this.initCause(initCause);
	}

	/**
	 * The url that caused the failure.
	 */
	public String getUrl() {
		return this.url;
	}

	/**
	 * The http client status code. A status code of -1 means that there isn't one
	 * and probably isn't a response phrase either.
	 */
	public int getStatusCode() {
		return this.statusCode;
	}

	/**
	 * The http client response phrase.
	 */
	public String getResponsePhrase() {
		return this.responsePhrase;
	}

  @Override
  public String toString()
  {
      return super.toString() + " (" + this.getStatusCode() + " " + responsePhrase + ")";
  }
}